<template>
  <div>
    <!--Breadcrumb面包屑组件-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row :gutter="20">
        <el-col :span="10">
          <!--clearable开启内容清除按钮-->
          <!--点击input组件中的clearable清除按钮时触发会触发clear事件-->
          <el-input
            placeholder="查找商品"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            >
            </el-button> </el-input
        ></el-col>
        <el-col :span="1.5">
          <el-button type="primary" @click="goAddGoodsPage">添加商品</el-button>
          <!--使用Dialog组件实现添加用户对话框布局-->
          <!--:visible.sync控制对话框显示或关闭，接收一个布尔值，true时显示false关闭-->
          <!--@close对话框关闭事件，在关闭对话框时会触发这个事件回调函数-->
        </el-col>
        <el-col :span="1.5"
          ><el-button @click="getGoodsList">刷 新</el-button></el-col
        >
      </el-row>

      <el-table :data="goodsList" border stripe class="goodsTable">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column
          width="110px"
          prop="goods_price"
          label="商品价格(元)"
        ></el-table-column>
        <el-table-column
          prop="goods_weight"
          label="商品重量"
          width="80px"
        ></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="140px">
          <template slot-scope="scope">
            {{ scope.row.add_time | formatDate }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="getEditGoodsDialog(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              @click="deleGoods(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[8, 15, 20, 25]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "goods",
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 8
      },
      goodsList: [],
      total: 0
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo
      });
      if (res.meta.status !== 200)
        this.$message.error("获取商品列表失败，请重试!");
      console.log(res.data.goods);
      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },
    handleSizeChange(size) {
      this.queryInfo.pagesize = size;
      this.getGoodsList();
    },
    handleCurrentChange(current) {
      this.queryInfo.pagenum = current;
      this.getGoodsList();
    },
    deleGoods(row) {
      this.$confirm("此操作将删除该商品，是否继续？", "删除商品", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(
            `goods/${row.goods_id}`
          );
          if (res.meta.status !== 200)
            return this.$message.error("删除失败，请重试!");
          this.getGoodsList();
          this.$message.success("删除成功!");
        })
        .catch(() => {
          this.$message("取消删除");
        });
    },
    goAddGoodsPage() {
      this.$router.push("/goods/addGoods");
    }
  }
};
</script>

<style scoped lang="less">
.goodsTable {
  margin: 15px 0;
}
</style>
